cmake_minimum_required(VERSION 3.15)
project(async_mysql LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 项目版本
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 0)
set(PROJECT_VERSION_PATCH 0)
set(PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})

# 配置选项
option(ASYNC_MYSQL_USE_BOOST_ASIO "Use Boost.Asio instead of standalone ASIO" OFF)
option(ASYNC_MYSQL_BUILD_TESTS "Build tests" OFF)

# 设置输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# 查找依赖项
find_package(Threads REQUIRED)

if(ASYNC_MYSQL_USE_BOOST_ASIO)
    # 使用 Boost.Asio
    find_package(Boost 1.66 REQUIRED COMPONENTS system)
    set(ASYNC_MYSQL_DEFINITIONS ASYNC_MYSQL_USE_BOOST_ASIO)
    add_compile_definitions(ASYNC_MYSQL_USE_BOOST_ASIO)
    message(STATUS "Using Boost.Asio")
else()
    # 使用独立 ASIO
    set(ASYNC_MYSQL_DEFINITIONS ASYNC_MYSQL_USE_STANDALONE_ASIO)
    add_compile_definitions(ASYNC_MYSQL_USE_STANDALONE_ASIO)
    message(STATUS "Using standalone ASIO")
endif()

# 查找 MySQL 开发库
find_package(MySQL REQUIRED)
if(MySQL_FOUND)
    message(STATUS "Found MySQL library: ${MySQL_LIBRARY}")
    message(STATUS "MySQL include dir: ${MySQL_INCLUDE_DIR}")
else()
    message(FATAL_ERROR "MySQL development libraries not found")
endif()

# 添加头文件库目标
add_library(async_mysql INTERFACE)
add_library(async_mysql::async_mysql ALIAS async_mysql)

# 设置头文件库属性
target_include_directories(async_mysql INTERFACE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
    $<INSTALL_INTERFACE:include>
)

# 添加依赖项
target_link_libraries(async_mysql INTERFACE
    Threads::Threads
    ${MySQL_LIBRARY}
)

if(ASYNC_MYSQL_USE_BOOST_ASIO)
    target_link_libraries(async_mysql INTERFACE Boost::system)
endif()

# 安装目标
install(TARGETS async_mysql EXPORT async_mysqlTargets)
install(
    DIRECTORY src/
    DESTINATION include
    FILES_MATCHING PATTERN "*.hpp"
)

# 导出配置文件
install(EXPORT async_mysqlTargets
    FILE async_mysqlTargets.cmake
    NAMESPACE async_mysql::
    DESTINATION lib/cmake/async_mysql
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    async_mysqlConfigVersion.cmake
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMajorVersion
)

install(
    FILES
        ${CMAKE_CURRENT_BINARY_DIR}/async_mysqlConfigVersion.cmake
    DESTINATION lib/cmake/async_mysql
)

# 测试支持
if(ASYNC_MYSQL_BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()


# 文档生成
find_package(Doxygen)
if(DOXYGEN_FOUND)
    set(DOXYGEN_PROJECT_NAME "AsyncMySQL")
    set(DOXYGEN_PROJECT_NUMBER ${PROJECT_VERSION})
    set(DOXYGEN_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/docs)
    set(DOXYGEN_GENERATE_LATEX NO)
    set(DOXYGEN_EXTRACT_ALL YES)
    
    file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp)
    
    doxygen_add_docs(docs
        ${HEADERS}
        COMMENT "Generating API documentation with Doxygen"
    )
endif()